﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Schema;

namespace LanguageFeatures.Models
{
    //public class MyExtensionMethods
    //{
    //}
    //上面三行默认产生-先忽略它
    public static class MyExtensionMethods
    {
        public static decimal TotalPrices(this IEnumerable<Product>productEnum) { 
        decimal total = 0;
        foreach (Product prod in productEnum)
	{
            total += prod.Price;
	}
    return total;
    }
    public static IEnumerable<Product> FilterByCategory(
    this IEnumerable<Product> productEnum, string categoryParam)
    {
        foreach (Product prod in productEnum)
        {
            if (prod.Category == categoryParam)
            {
                yield return prod;
            }
        }
    }

    //使用Lambda表达式
    public static IEnumerable<Product> Filter(
        this IEnumerable<Product> productEnum, Func<Product, bool> selectorParma)
        {
            foreach (Product prod in productEnum)
            {
                if (selectorParma(prod))
                {
                    yield return prod;
                }
            }
        }

    }
}